package com.jackaltsc.mydemolist.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.jackaltsc.mydemolist.R;
import com.jackaltsc.mydemolist.adapter.RecyclerAdapter;
import com.jackaltsc.mydemolist.myview.DividerViewGrid;
import com.jackaltsc.mydemolist.myview.DividerViewLinear;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by JackalTsc on 2016/6/11.
 */
public class RecyclerViewActivity extends Activity implements View.OnClickListener {

    private RecyclerView mRecyclerView;
    private List<String> mDatas;
    private RecyclerAdapter mAdapter;

    private Button btnAdd, btnDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_recyclerview);


        initView();
    }

    private void initView() {

        getData();

        btnAdd = (Button) findViewById(R.id.btn_add);
        btnDelete = (Button) findViewById(R.id.btn_delete);
        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);

        mRecyclerView = (RecyclerView) findViewById(R.id.rv_demo);

        //设置线性布局管理器
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));

        mAdapter = new RecyclerAdapter(this, mDatas);
        mRecyclerView.setAdapter(mAdapter);

        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        mRecyclerView.addItemDecoration(new DividerViewLinear(this, DividerViewLinear.VERTICAL_LIST));
        mRecyclerView.addItemDecoration(new DividerViewGrid(this));


    }

    protected void getData() {
        mDatas = new ArrayList<>();
        for (int i = 'A'; i < 'z'; i++) {
            mDatas.add("" + (char) i);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_add:
                addData(3);
                break;
            case R.id.btn_delete:
                removeData(3);
                break;
        }

    }

    public void addData(int position) {
        mDatas.add(position, "AAA");
        mAdapter.notifyItemInserted(position);
    }

    public void removeData(int position) {
        mDatas.remove(position);
        mAdapter.notifyItemRemoved(position);
    }
}
